package com.subtlman.logframework.queue;

import com.subtlman.logframework.utils.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Create: IntelliJ IDEA.
 * @Author: subtlman_ljx
 * @Date: 2022/11/18/15:57
 * @Description:
 */
@Slf4j
@Component
public class DealQueueThread implements Runnable {

    private static DealQueueThread dealQueueThread;

//    @Autowired
//    BuyGoodService buyGoodService;
//
//    @Autowired
//    BuyOrdersService buyOrdersService;
//
//    @Autowired
//    JedisPool jedisPool;
//
//    private Jedis jedis;

    private BuyQueue buyqueue;

    //线程的默认执行标志为未执行,即空闲状态
    public static boolean excute = false;

    public DealQueueThread() {

    }

    public DealQueueThread(BuyQueue buyqueue) {
        this.buyqueue = buyqueue;
//        jedis = dealQueueThread.jedisPool.getResource();

    }

    @PostConstruct
    public void init() {
        dealQueueThread = this;
//        dealQueueThread.buyGoodService = this.buyGoodService;
//        dealQueueThread.buyOrdersService = this.buyOrdersService;
//        dealQueueThread.jedisPool = this.jedisPool;
    }

    int i=0,j=0;

    @Override
    public void run() {
        try {
            //修改线程的默认执行标志为执行状态
//            excute = true;
            int temp = 3;
            i++;
            //开始处理请求队列中的请求,按照队列的FIFO的规则,先处理先放入到队列中的请求
            while (temp>0) {
                //取出队列中的请求
                temp--;
                Thread.sleep(5000);
                j++;
                log.info("run执行"+temp+" -- "+i+" -- "+j);
//                BuyRequest buyreq = buyqueue.take();
                //处理请求
//                dealWithQueue(buyreq);
            }
            log.info("方法前："+Thread.activeCount());
            Util.waitTime();
            log.info("方法后："+Thread.activeCount());
        } catch (Exception e) {
            System.out.println(e);
        } finally {

//            excute = false;
        }
    }

    public synchronized void dealWithQueue(BuyRequest buyreq) {
        try {
            //为了尽量确保数据的一致性,处理之前先从redis中获取当前抢购商品的剩余数量
            //给予当前商品数量定值
            int residue = 3;
            //如果没有剩余商品,就直接返回
            if (residue < 1) {
                buyreq.setResponse_status(3);
                return;
            }

            //如果有剩余商品,先在redis中将剩余数量减一,再开始下订单
//            jedis.decr("residue" + buyreq.getGood_id());
            //将数据库中将剩余数量减一,这一步处理可以在队列处理完成之后一次性更新剩余数量
//            dealQueueThread.buyGoodService.minusResidue(buyreq.getGood_id());
            //处理请求,下订单
            BuyOrders bo = new BuyOrders();
//            bo.setGood_id(buyreq.getGood_id());
//            bo.setUser_id(buyreq.getUser_id());
//            int order_id = dealQueueThread.buyOrdersService.insert(bo);
//            BuyOrders orders = dealQueueThread.buyOrdersService.getById(order_id);
//            //订单id
//            buyreq.setOrder_id(order_id);
//            //订单信息
//            buyreq.setBuyOrders(orders);
            //处理完成状态
            buyreq.setResponse_status(1);
        } catch (Exception e) {
            //异常状态
            buyreq.setResponse_status(2);
            System.out.println(e);
        }
    }
}
